package cn.mju.tankfighterbackend.config.security;


import cn.mju.tankfighterbackend.config.security.handler.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
@EnableWebSecurity
@Configuration

public class SecurityConfig {
    @Autowired
    UserAuthenticationFailureHandler failureHandler;
    @Autowired
    UserAuthenticationSuccessHandler successHandler;
    @Autowired
    UserLogOutSuccessHandler logOutSuccessHandler;
    @Autowired
    UserServiceImpl service;

    @Autowired
    UserAuthenticationEntryPoint userAuthenticationEntryPoint;

    @Autowired
    UserAccessDeniedHandler userAccessDeniedHandler;

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder(4);
    }



/*    @Bean
    public CorsFilter corsFilter() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        if (cors) {
            corsConfiguration.applyPermitDefaultValues();
            corsConfiguration.addAllowedOriginPattern("http://localhost:3000");
        }
        corsConfiguration.setAllowCredentials(true);
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsFilter(source);
    }*/

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        return http

                .exceptionHandling()
                .accessDeniedHandler(userAccessDeniedHandler)
                .authenticationEntryPoint(userAuthenticationEntryPoint)
                .and()
                .formLogin()
                .loginProcessingUrl("/user/login")
                .usernameParameter("username")
                .passwordParameter("password")
                .successHandler(successHandler)
                .failureHandler(failureHandler)
                .and()
                .logout()
                .logoutUrl("/logout")
                .logoutSuccessHandler(logOutSuccessHandler)
                .and()

                .cors()
                .and()
                .csrf()
                .disable()
                .build();


    }
//    @Bean
//    public CookieSerializer httpSessionIdResolver(){
//        DefaultCookieSerializer cookieSerializer = new DefaultCookieSerializer();
//        cookieSerializer.setCookieName("token");
//        cookieSerializer.setUseHttpOnlyCookie(false);
//        cookieSerializer.setSameSite(null);
//        return cookieSerializer;
//    }

}
